package CompleteFutureDemo;

import common.CalcTask;
import common.PostProcess;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;

public class ThenApplyTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinPool joinPool = new ForkJoinPool();
        CompletableFuture<Double> cf1 = CompletableFuture.supplyAsync(() -> new CalcTask().RunWithResult(),joinPool);
        CompletableFuture<String> cf2 = cf1.thenApplyAsync((result) -> {
            System.out.println(Thread.currentThread()+"后处理开始："+System.currentTimeMillis());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread()+"后处理结束："+System.currentTimeMillis());
            return "test:"+result;
        });
        System.out.println("主线程执行"+System.currentTimeMillis());
        System.out.println("任务执行结果"+cf1.get());
        System.out.println("后处理结果为："+cf2.get());
        System.out.println("主线程结束："+System.currentTimeMillis());
    }
}
